/*
 * Copyright © 2017 eqxiu.com 北京中网易企秀科技有限公司  All rights reserved.
 */
package cn.hermit.data.xml.model;

import java.util.Collection;
import java.util.Iterator;

import javax.xml.namespace.QName;

import cn.hermit.util.ReflectionUtils;
import cn.hermit.util.StringUtils;

/**
 * Java model of XML document.
 * 
 * @author Jack Gao (Chinese name : GAO JIANGUO, Email : linux.gjg@gmail.com)
 * @date 19 Jun, 2014
 */
public final class XmlDocument extends XmlElement {

	/**
	 * 
	 */
	private static final long serialVersionUID = 744147819937837199L;

	XmlHeader header;

	XmlElement root;

	XmlDocument() {
	}

	/**
	 * Get XML header from Document.
	 * 
	 * @return The XML header
	 */
	public XmlHeader getHeader() {
		return header;
	}

	/**
	 * Get root element of Document.
	 * 
	 * @return The root element
	 */
	public XmlElement getRoot() {
		return root;
	}

	/**
	 * Not support.
	 */
	public int attributeSize() {
		throw new UnsupportedOperationException();
	}
	/**
	 * Not support.
	 */
	public Iterator<XmlAttribute> attributeEntryIterator() {
		throw new UnsupportedOperationException();
	}
	/**
	 * Not support.
	 */
	@Override
	public Collection<XmlAttribute> getNamespaceAttributes() {
		throw new UnsupportedOperationException();
	}
	/**
	 * Not support.
	 */
	@Override
	public XmlAttribute getAttribute(QName name) {
		throw new UnsupportedOperationException();
	}
	/**
	 * Not support.
	 */
	@Override
	public Collection<XmlElement> getElements() {
		throw new UnsupportedOperationException();
	}
	/**
	 * Not support.
	 */
	@Override
	public QName getName() {
		throw new UnsupportedOperationException();
	}
	/**
	 * Not support.
	 */
	public void clearAttributes() {
		throw new UnsupportedOperationException();
	}
	/**
	 * Not support.
	 */
	public void clearChildren() {
		throw new UnsupportedOperationException();
	}

	/**
     * Serialize XML Document to XML string.
     */
	public String toString() {
		StringBuilder result = new StringBuilder();
		if (this.header != null)
			result.append(this.header.toString());
		if (!ReflectionUtils.isEmpty(children)) {
			Iterator<XmlNode> iter = this.children.iterator();
			while (iter.hasNext()) {
				XmlNode child = iter.next();
				result.append(child.toString());
			}
		}
		return result.toString();
	}

	public String toFormatString(int depth, String newLineFeed,
			boolean newLineForTexts) {
		StringBuilder b = new StringBuilder();
		if (this.header != null) {
			b.append(StringUtils.tabs(depth)).append(this.header.toString());
		}
		if (!ReflectionUtils.isEmpty(children)) {
			if (this.header != null)
				b.append(newLineFeed);
			Iterator<XmlNode> iter = this.children.iterator();
			while (iter.hasNext()) {
				XmlNode child = iter.next();
				b.append(child.toFormatString(depth, newLineFeed,
						newLineForTexts));
				if (iter.hasNext())
					b.append(newLineFeed);
			}
		}
		return b.toString();
	}

	@Override
	public boolean isDocument() {
		return true;
	}

	@Override
	public boolean isElement() {
		return false;
	}

	@Override
	public boolean isNameNode() {
		return false;
	}

}